# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=nlopt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.10.0
pkgrel=1
pkgdesc="A library for nonlinear optimization (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://nlopt.readthedocs.io/en/latest/'
msys2_repository_url="https://github.com/stevengj/nlopt"
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("https://github.com/stevengj/nlopt/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('506f83a9e778ad4f204446e99509cb2bdf5539de8beccc260a014bd560237be1')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare () {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C $srcdir || true
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake.exe" \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DPython_EXECUTABLE="${MINGW_PREFIX}/bin/python" \
    -DBUILD_SHARED_LIBS=ON \
    -DNLOPT_JAVA=OFF \
    -DNLOPT_PYTHON=ON \
    -DNLOPT_SWIG=ON \
    -DNLOPT_GUILE=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}

  # DLLs from gcc-libs need to be copied to test folder
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./
}
